<?php
	class UserAction extends Action {
		private $user;
		function __construct(){
			parent::__construct();
			$this->user=new UserModel();
		}
		function  add($tmess=null,$status=0){
			if(empty($tmess)){
				$this->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息. ');
			}else{
				$this->message($status,$tmess);
			}
			if($_POST["sex"]=="1"){
				$this->assign("male", "checked");
			}elseif($_POST["sex"]=="-1"){
				$this->assign("female", "checked");
			}else{
				$this->assign("secret", "checked");
			}
			
			$this->assign("fbpl", "checked");
			
			$this->assign("post", $_POST);	
			$this->display("admin/user/add.tpl");
		}

		function insert(){
			if($this->user->existsUser($_POST["userName"])){
				$this->add("该用户已经存在",0);
			}else{
				$_POST["regTime"]=time();	
				if($this->user->validateForm()){
					$_POST["userPwd"]=md5($_POST["userPwd"]);
					if($this->user->add($_POST)){
						$_POST=array();
						$this->add("用户添加成功",1);
				
					}else{
						$this->add("用户链接添加失败.",0);
					}
				}else{
					$this->add($this->user->getMessList(),0);
				}
			}

		}


		function edit($tmess=null,$status=0, $num=0){
			if(empty($tmess)){
				$this->assign("tmess", '管理员用户不能删除，否则将不能登录系统,红色用户名为禁用用户.<br> 用户被删除时,他的评论也会一起删除. ');
			}else{
				$this->message($status,$tmess);
			}

			if($num){
				$page=new Page("admin.php?m=user&a=search&username=".$_REQUEST["username"]."&",$num, ARTICLE_PAGE_SIZE);
				$users=$this->user->findAll(array("id,userName, email, regTime,disable"),$page->getOffset(), ARTICLE_PAGE_SIZE, "where userName like '%".$_REQUEST["username"]."%'");
				$this->assign("username",$_REQUEST["username"]);
			}else{	
				$page=new Page("admin.php?m=user&a=edit&",$this->user->total(), ARTICLE_PAGE_SIZE);
				$users=$this->user->findAll(array("id,userName, email, regTime,disable"),$page->getOffset(), ARTICLE_PAGE_SIZE);
			}	
		        
		
			$this->assign("users", $users);
			$this->assign("page", isset($_GET["page"])?$_GET["page"]:1);
			$this->assign("fpage", $page->getPage());
			$this->assign("username",$_REQUEST["username"]);
			$this->display("admin/user/edit.tpl");
		}
		function search(){
			$num=$this->user->total("where userName like '%".$_REQUEST["username"]."%'");
		
			if($num){
				$this->edit("符合搜索条件的用户有 <b>".$num."</b> 个！",1, $num);
			}else{
				$this->edit("没有搜索到符合条件的用户,列出所有用户！",0);
			}
		}
		function del(){
			if($this->user->del($_REQUEST["id"])){
				$this->edit("删除用户成功！",1);
			}else{
				$this->edit("删除用户失败！",0);
			}
		}



		function mod($tmess=null,$status=1){
			if(empty($tmess)){
				$this->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息. ');
			}else{
				$this->message($status,$tmess);
			}

			if($status){
				$user=$this->user->find($_REQUEST["id"]);
				$this->assign("post", $user);
			
			}else{
				$user=$_POST;
			}
			if($user["sex"]=="1"){
				$this->assign("male", "checked");
			}elseif($user["sex"]=="-1"){
				$this->assign("female", "checked");
			}else{
				$this->assign("secret", "checked");
			}

			if($user["disable"]=="1"){
				$this->assign("disable", "checked");
			}
			if($user["allow_dlht"]=="1"){
				$this->assign("dlht", "checked");
			}
		
			if($user["allow_fbpl"]=="1"){
				$this->assign("fbpl", "checked");
			}
		
			$this->assign("post", $user);
			$this->display("admin/user/mod.tpl");
		}

		function update(){
			if($this->user->validateForm(0)){
				if(empty($_POST["userPwd"])){
					unset($_POST["userPwd"]);
				}else{
					$_POST["userPwd"]=md5($_POST["userPwd"]);
				}
			
				if(!isset($_POST["disable"])){
					$_POST["disable"]=0;	
				}
				if(!isset($_POST["allow_dlht"])){
					$_POST["allow_dlht"]=0;	
				}
				if(!isset($_POST["allow_fbpl"])){
					$_POST["allow_fbpl"]=0;	
				}
			
			
				if($this->user->mod($_POST)){
					$this->mod("修改成功",1);
				}else{
					$this->mod("修改失败",0);
				}	 
			}else{
				$this->mod($this->user->getMessList(), 0);
			}		
		
		
		}



		function login(){
			$user=new UserModel();
			$log	= $user->login($_POST['uname'], $_POST['pwd']); //检验管理员登录
			if ($log==1){ //登录成功
				$this->redirect("admin.php", "index", "index");
				exit;	
			}else { //登录失败
				$message = "用户名或密码错误, 请重试.";
				$this->assign("message", $message);
				$this->assign("className", "login-msg");
				$this->display("admin/user/login.tpl");
			}

		}

	}
